<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Users;
use App\Services\CommonService;
use App\Services\OutputService;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Redis;
use Auth;

class LoginController extends Controller
{
    public function login(Request $request)
    {
        return view("admin.login.login");
    }

    public function loginSubmit(Request $request)
    {
        if (empty($request->username) || empty($request->password)) return back()->with("status","error")->with("msg","账号或者密码为空！");
        $username = str_replace(" ","",$request->username);
        //校验是否存在
        $users = Users::getUserByPhone($username);
        //没有命中，返回错误提示
        if (empty($users)) {
            $password = Hash::make($request->password);
            $users_id = Users::creatUsers($username,$password);
        } else {
            if (!password_verify($request->password, $users->password)) return back()->with("status","error")->with("msg","您输入的密码错误！");
            //账号已被禁用
            if ($users->disable == 2) return back()->with("status","error")->with("msg","对不起，此账号已被禁用");
            $users_id = $users->id;
        }
        // 验证成功，加入auth
        Auth::loginUsingId($users_id);
        return redirect("/admin/short/list");
    }
}
